home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / SCHMOO.ZIP / FILE.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  11KB  |  458 lines

  1. /*
  2.  * FILE.C
  3.  *
  4.  * Functions for handling dirty files and processing File menu commands.
  5.  *
  6.  * Functions:
  7.  *  FDirtySet, FCleanVerify
  8.  *  FFileNew, FFileOpen, FFileSave, FFileSaveAs, FFileExit
  9.  *
  10.  * This file contains the only functions that manipulate the fDirty flag.
  11.  *
  12.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  13.  *
  14.  */
  15.  
  16. #include <windows.h>
  17. #include <ole.h>
  18. #include "schmoo.h"
  19. #include "oleglobl.h"
  20.  
  21.  
  22.  
  23.  
  24.  
  25. /*
  26.  * FDirtySet
  27.  *
  28.  * Purpose:
  29.  *  Sets or clears the global 'dirty' flag returning the previous state
  30.  *  of that same flag.  Even though the non-OLE part of this function
  31.  *  is trivial, it isolates handling changes, providing a single point
  32.  *  to notify a client app with OLE_CHANGED.
  33.  *
  34.  *  This function does exits if pGlob->fNoDirty is set.
  35.  *
  36.  * Parameters:
  37.  *  fDirty          BOOL used to set the value of the pGLob->fDirty flag.
  38.  *                  This allows us to use this function to both set and
  39.  *                  clear the flag.  OLE_CHANGED is only sent if the
  40.  *                  flag is set to TRUE.
  41.  *
  42.  * Return Value:
  43.  *  BOOL            Previous value of the dirty flag.
  44.  */
  45.  
  46. BOOL FAR PASCAL FDirtySet(BOOL fDirty)
  47.     {
  48.     BOOL        fPrevious;
  49.  
  50. #ifdef MAKEOLESERVER
  51.     /*
  52.      * If we are a hidden window, then there's nothing that could make
  53.      * us dirty since there cannot be any user interaction here.  Therefore
  54.      * ignore any changes to the dirty flag, leaving it FALSE.
  55.      */
  56.     if (!IsWindowVisible(pGlob->hWnd))
  57.         return pGlob->fDirty;
  58.  
  59. #endif //MAKEOLESERVER
  60.  
  61.     if (pGlob->fNoDirty)
  62.         return pGlob->fDirty;
  63.  
  64.     fPrevious=pGlob->fDirty;
  65.     pGlob->fDirty=fDirty;
  66.  
  67. #ifdef MAKEOLESERVER
  68.     if (fDirty)
  69.         //Fun indirection, huh?  That what you get with an OOP.
  70.         OLEClientNotify(pOLE->pSvr->pDoc->pObj, OLE_CHANGED);
  71. #endif //MAKEOLESERVER
  72.  
  73.     return fPrevious;
  74.     }
  75.  
  76.  
  77.  
  78.  
  79.  
  80. /*
  81.  * FCleanVerify
  82.  *
  83.  * Purpose:
  84.  *  Checks the pGLob->fDirty flag, and if set, displays a message
  85.  *  box informing the user that the file is dirty and asking if
  86.  *  the file should be saved or updated.  If YES is chosen, the file
  87.  *  is saved or updated.
  88.  *
  89.  * Parameters:
  90.  *  pGlob           LPGLOBALS to global variable block.
  91.  *
  92.  * Return Value:
  93.  *  BOOL            TRUE if it's safe to proceed with the operation (file
  94.  *                  is clean, user answered NO, or file was saved).
  95.  *                  FALSE if the user wants to cancel the operation or there
  96.  *                  was an error.
  97.  */
  98.  
  99. BOOL FAR PASCAL FCleanVerify(LPGLOBALS pGlob)
  100.     {
  101.     BOOL        fRet=TRUE;
  102.     WORD        wRet;
  103.     char       *psz;
  104.  
  105. #ifdef MAKEOLESERVER
  106.     char        szClient[40];
  107. #endif //MAKEOLESERVER
  108.  
  109.     //Nothing to do if we're clean.
  110.     if (!pGlob->fDirty)
  111.         return TRUE;
  112.  
  113.     if (!pGlob->fOLE)
  114.         {
  115.         wRet=MessageBox(pGlob->hWnd, rgpsz[IDS_FILEDIRTY],
  116.                         rgpsz[IDS_CAPTION], MB_YESNOCANCEL | MB_ICONEXCLAMATION);
  117.         }
  118. #ifdef MAKEOLESERVER
  119.     if (pGlob->fOLE)
  120.         {
  121.         //Linking case, use the same message as before.
  122.         if (pOLE->pSvr->fLink)
  123.             {
  124.             wRet=MessageBox(pGlob->hWnd, rgpsz[IDS_FILEDIRTY],
  125.                             rgpsz[IDS_CAPTION], MB_YESNOCANCEL);
  126.             }
  127.  
  128.         //Embedding: Ask the user about updating instead of saving.
  129.         if (pOLE->pSvr->fEmbed)
  130.             {
  131.             //Build the standard string for Updating.
  132.             psz=(PSTR)LocalAlloc(LPTR, 1024);
  133.  
  134.             if (NULL==psz)
  135.                 return FALSE;
  136.  
  137.             GetAtomName(pOLE->pSvr->pDoc->aClient, szClient, sizeof(szClient));
  138.  
  139.             lstrcpy(psz, rgpsz[IDS_CLOSEALERT1]);
  140.             lstrcat(psz, szClient);
  141.             lstrcat(psz, rgpsz[IDS_CLOSEALERT2]);
  142.  
  143.             wRet=MessageBox(pGlob->hWnd, psz, rgpsz[IDS_CAPTION],
  144.                             MB_YESNOCANCEL | MB_ICONEXCLAMATION);
  145.  
  146.             LocalFree((HANDLE)psz);
  147.             }
  148.         }
  149. #endif //MAKEOLESERVER
  150.  
  151.     switch (wRet)
  152.         {
  153.         case IDCANCEL:
  154.             fRet=FALSE;
  155.             break;
  156.  
  157.         case IDNO:
  158.             fRet=TRUE;
  159.             break;
  160.  
  161.         case IDYES:
  162.             if (!pGlob->fOLE)
  163.                 fRet=FFileSave(pGlob);
  164.  
  165. #ifdef MAKEOLESERVER
  166.             //Linking same as stand-alone.
  167.             if (pOLE->pSvr->fLink)
  168.                 fRet=FFileSave(pGlob);
  169.  
  170.             if (pOLE->pSvr->fEmbed)
  171.                 OLEClientNotify(pOLE->pSvr->pDoc->pObj, OLE_CLOSED);
  172.  
  173. #endif //MAKEOLESERVER
  174.             break;
  175.         }
  176.  
  177.     return fRet;
  178.     }
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. /*
  186.  * FFileNew
  187.  *
  188.  * Purpose:
  189.  *  Confirms the new file with the user and cleans out the Polyline
  190.  *  image.
  191.  *
  192.  * Parameters:
  193.  *  pGlob           LPGLOBALS to the global variable block.
  194.  *
  195.  * Return Value:
  196.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  197.  *
  198.  */
  199.  
  200. BOOL FAR PASCAL FFileNew(LPGLOBALS pGlob)
  201.     {
  202.     if (!FCleanVerify(pGlob))
  203.         return FALSE;
  204.  
  205. #ifdef MAKEOLESERVER
  206.     PDocRevokeAndCreate(pOLE);
  207. #endif //MAKEOLESERVER
  208.  
  209.     pGlob->fOLE=FALSE;
  210.  
  211.     SendMessage(pGlob->hWndPolyline, PLM_POLYLINENEW, 0, 0L);
  212.     pGlob->fDirty=FALSE;
  213.     pGlob->fOpenFile=FALSE;
  214.  
  215.     WindowTitleSet(pGlob->hWnd, rgpsz[IDS_UNTITLED]);
  216.     return TRUE;
  217.     }
  218.  
  219.  
  220.  
  221.  
  222. /*
  223.  * FFileOpen
  224.  *
  225.  * Purpose:
  226.  *  Confirms that the user wants to open a new file and invokes the
  227.  *  common dialog file open to get the filename, then reads the
  228.  *  contents of the file.  If the fImport flag is TRUE, then we
  229.  *  import the contents of the file into the current document,
  230.  *  not changing the document name or any of the UI.
  231.  *
  232.  * Parameters:
  233.  *  pGlob           LPGLOBALS to the global variable block.
  234.  *  fImport         BOOL indicates if we're importing from a file,
  235.  *                  not affecting the current filename.
  236.  *
  237.  * Return Value:
  238.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  239.  *
  240.  */
  241.  
  242. BOOL FAR PASCAL FFileOpen(LPGLOBALS pGlob, BOOL fImport)
  243.     {
  244.     POLYLINE        pl;
  245.     BOOL            fOK;
  246.     LPSTR           psz;
  247.     char            szTemp[CCHPATHMAX];
  248.     LPSTR           pszFile;
  249.  
  250.     if (!fImport)
  251.         {
  252.         if (!FCleanVerify(pGlob))
  253.             return FALSE;
  254.         }
  255.  
  256.     psz=(fImport) ? rgpsz[IDS_FILEIMPORT] : rgpsz[IDS_FILEOPEN];
  257.  
  258.     //We have to use a temp for Import or else we'll wipe out a real filename.
  259.     pszFile=(fImport) ? (LPSTR)szTemp : (LPSTR)pGlob->szFile;
  260.  
  261.     fOK=FSaveOpenDialog(pGlob->hWnd, pGlob->hInst, rgpsz[IDS_DEFEXT],
  262.                         rgpsz[IDS_FILEOPENFILTER], pszFile, psz, TRUE);
  263.  
  264.     if (fOK)
  265.         {
  266.         //Attempt to read the file in and display it.
  267.         if (!FMooFileRead(pGlob, pszFile, &pl))
  268.             return FALSE;
  269.  
  270. #ifdef MAKEOLESERVER
  271.         if (!fImport)
  272.             PDocRevokeAndCreate(pOLE);
  273. #endif //MAKEOLESERVER
  274.  
  275.         SendMessage(pGlob->hWndPolyline, PLM_POLYLINESET,
  276.                     TRUE, (LONG)(LPSTR)&pl);
  277.  
  278.         if (!fImport)
  279.             {
  280.             WindowTitleSet(pGlob->hWnd, pszFile);
  281.             pGlob->fOLE=FALSE;
  282.  
  283.             pGlob->fOpenFile=TRUE;
  284.             pGlob->fDirty=FALSE;
  285.             }
  286.         else
  287.             pGlob->fDirty=TRUE;
  288.         }
  289.  
  290.     return fOK;
  291.     }
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298. /*
  299.  * FFileSave
  300.  *
  301.  * Purpose:
  302.  *  Writes the file to a known filename, requiring that the user has
  303.  *  previously used FileOpen or FileSaveAs in order to have a filename.
  304.  *
  305.  * Parameters:
  306.  *  pGlob           LPGLOBALS to the global variable block.
  307.  *
  308.  * Return Value:
  309.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  310.  *
  311.  */
  312.  
  313. BOOL FAR PASCAL FFileSave(LPGLOBALS pGlob)
  314.     {
  315.     POLYLINE        pl;
  316.  
  317. #ifdef MAKEOLESERVER
  318.     OLESTATUS       os;
  319.  
  320.     //In OLE cases, this may be Update; call OleSavedServerDoc
  321.     if (pGlob->fOLE)
  322.         {
  323.         if (pOLE->pSvr->fLink)
  324.             {
  325.             SendMessage(pGlob->hWndPolyline, PLM_POLYLINEGET, 0,
  326.                         (LONG)(LPSTR)&pl);
  327.  
  328.             if (!FMooFileWrite(pGlob, pGlob->szFile, &pl))
  329.                 return FALSE;
  330.             }
  331.  
  332.         //This notifies the client for us.
  333.         os=OleSavedServerDoc(pOLE->pSvr->pDoc->lh);
  334.  
  335.         pGlob->fDirty=(BOOL)(OLE_OK!=os);
  336.         return !pGlob->fDirty;
  337.         }
  338.  
  339. #endif //MAKEOLESERVER
  340.  
  341.     if (!pGlob->fOpenFile)
  342.         return FFileSaveAs(pGlob);
  343.  
  344.     SendMessage(pGlob->hWndPolyline, PLM_POLYLINEGET, 0, (LONG)(LPSTR)&pl);
  345.  
  346.     if (!FMooFileWrite(pGlob, pGlob->szFile, &pl))
  347.         return FALSE;
  348.  
  349.     pGlob->fOpenFile=TRUE;
  350.     pGlob->fDirty=FALSE;
  351.     return TRUE;
  352.     }
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359. /*
  360.  * FFileSaveAs
  361.  *
  362.  * Purpose:
  363.  *  Invokes the common dialog for Save As to get a filename then
  364.  *  writes the polyline data to that file, creating if necessary.
  365.  *
  366.  * Parameters:
  367.  *  pGlob           LPGLOBALS to the global variable block.
  368.  *
  369.  * Return Value:
  370.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  371.  *
  372.  */
  373.  
  374. BOOL FAR PASCAL FFileSaveAs(LPGLOBALS pGlob)
  375.     {
  376.     POLYLINE        pl;
  377.     BOOL            fOK;
  378.  
  379.     fOK=FSaveOpenDialog(pGlob->hWnd, pGlob->hInst, rgpsz[IDS_DEFEXT],
  380.                         rgpsz[IDS_FILEOPENFILTER], pGlob->szFile,
  381.                         rgpsz[IDS_FILESAVEAS], FALSE);
  382.  
  383.     if (fOK)
  384.         {
  385.         SendMessage(pGlob->hWndPolyline, PLM_POLYLINEGET,
  386.                     0, (LONG)(LPSTR)&pl);
  387.  
  388.         fOK=FMooFileWrite(pGlob, pGlob->szFile, &pl);
  389.  
  390. #ifdef MAKEOLESERVER
  391.         /*
  392.          * In the Save Copy As case, when an object is embedded, we
  393.          * don't need to call Rename or Saved since we just saved a
  394.          * silent copy of the data.
  395.          */
  396.         if (pGlob->fOLE && pOLE->pSvr->fEmbed)
  397.             return fOK;
  398.  
  399.         if (fOK && pGlob->fOLE && pOLE->pSvr->fLink)
  400.             {
  401.             OleRenameServerDoc(pOLE->pSvr->pDoc->lh, pGlob->szFile);
  402.             OleSavedServerDoc(pOLE->pSvr->pDoc->lh);
  403.             }
  404. #endif //MAKEOLESERVER
  405.  
  406.         pGlob->fOpenFile=fOK;
  407.         WindowTitleSet(pGlob->hWnd, pGlob->szFile);
  408.         pGlob->fDirty=FALSE;
  409.         }
  410.  
  411.     return fOK;
  412.     }
  413.  
  414.  
  415.  
  416. /*
  417.  * FFileExit
  418.  *
  419.  * Purpose:
  420.  *  Handles the File/Exit menu command, verifying dirty files as necessary
  421.  *  and revoking the server.
  422.  *
  423.  * Parameters:
  424.  *  pGlob           LPGLOBALS to the global variable block.
  425.  *
  426.  * Return Value:
  427.  *  BOOL            TRUE if the application can exit, FALSE otherwise.
  428.  */
  429.  
  430. BOOL FAR PASCAL FFileExit(LPGLOBALS pGlob)
  431.     {
  432.     BOOL            fRet;
  433. #ifdef MAKEOLESERVER
  434.     OLESTATUS       os;
  435.     LHSERVER        lhT;
  436. #endif //MAKEOLESERVER
  437.  
  438.     if (!FCleanVerify(pGlob))
  439.         return FALSE;
  440.  
  441. #ifdef MAKEOLESERVER
  442.     lhT=pOLE->pSvr->lh;
  443.     pOLE->pSvr->lh=0L;
  444.     os=OleRevokeServer(lhT);
  445.  
  446.     if (OLE_WAIT_FOR_RELEASE==os)
  447.         {
  448.         pOLE->pSvr->fRelease=FALSE;
  449.         FOLEReleaseWait(&pOLE->pSvr->fRelease, lhT);
  450.         fRet=TRUE;
  451.         }
  452.     else
  453.         fRet=(OLE_OK==os);
  454. #endif //MAKEOLESERVER
  455.  
  456.     return fRet;
  457.     }
  458.